home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-21 | 12.7 KB | 517 lines | [TEXT/ALFA] |
-
- ################################################################################
- # Shell routines.
- ################################################################################
-
-
- proc setShellMode {} {
- setTclMode
- changeMode "Csh"
- insertMenu "Tcl"
- }
-
- proc initShell {} {
- insertText "Welcome to Alpha's Tcl shell."
- insertText -w [lindex [winNames] 0] [shellPrompt]
- }
-
- # Return the prompt. We want the window name because some of the commands
- # we evaluate (such as 'edit') open a new window, and we want the insertion
- # to be done in the shell window.
- proc shellPrompt {} {
- regexp "(\[^:\]*):$" [pwd] crDum crDir
- return "\r«$crDir» "
- }
-
-
- # Called at all carriage returns.
- proc carriageReturn {} {
- global mode
- global indentOnCR
- set indentString ""
- deleteText [getPos] [selEnd]
- if {$indentOnCR} {
- set pos [getPos]
- set text [getText [lineStart $pos] $pos]
- for {set i 0; set len [string length $text]} {$i <= $len} {incr i} {
- set c [string index $text $i]
- if {($c != "\t") && ($c != "\ ")} {
- set indentString [string range $text 0 [expr $i-1]]
- break
- }
- }
- }
- insertText "\r" $indentString
- }
-
-
- proc tclCarriageReturn {} {
- global mode histnum
- global _text
- global _returnText
- set pos [getPos]
-
- if {![catch {regexp {∞} [getText $pos [nextLineStart $pos]]} res] && $res} {
- gotoMatch; return;
- }
- set ind [string first "»" [getText [lineStart $pos] $pos]]
- if {$ind < 0} {
- carriageReturn
- return
- }
- set lStart [expr [lineStart $pos]+$ind+2]
- endOfLine
- set _text [getText $lStart [getPos]]
- set fileName [lindex [winNames] 0]
- if {[getPos] != [maxPos]} {
- goto [maxPos]
- insertText -w $fileName $_text
- }
- if {[string first "Toolserver" $fileName] != -1} {
- if {![catch {dosc -n ToolServer -s $_text} _returnText]} {
- insertText "\r" $_returnText
- } else {
- insertText "\r"
- }
- mpwPrompt
- } else {
- uplevel #0 {catch $_text _returnText}
- history add $_text
- if {[string length $_returnText]} {
- insertText -w $fileName "\r" $_returnText [shellPrompt]
- } else {
- insertText -w $fileName [shellPrompt]
- }
- set histnum [history nextid]
- }
- unset _text
- unset _returnText
- }
- bind '\r' carriageReturn
- bind '\r' tclCarriageReturn "Csh"
- bind '\r' tclCarriageReturn "MPW"
-
-
- bind up <z> prevHist Csh
- bind down <z> nextHist Csh
-
- proc prevHist {} {
- global histnum
-
- set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
- if {[set ind [string first "» " $text]] > 0} {
- goto [expr [lineStart [getPos]] + $ind + 2]
- } else return
-
- incr histnum -1
- if {[catch {history event $histnum} text]} {
- incr histnum
- endOfLine
- return
- }
- set to [nextLineStart [getPos]]
- if {[lookAt [expr $to-1]] == "\r"} {incr to -1}
- replaceText [getPos] $to $text
- }
-
-
- proc nextHist {} {
- global histnum
-
- set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
- if {[set ind [string first "» " $text]] > 0} {
- goto [expr [lineStart [getPos]] + $ind + 2]
- } else return
-
- incr histnum
- if {[catch {history event $histnum} text]} {
- incr histnum -1
- endOfLine
- return
- }
- set to [nextLineStart [getPos]]
- if {[lookAt [expr $to-1]] == "\r"} {incr to -1}
- replaceText [getPos] $to $text
- }
-
-
- proc startMPW {} {
- global toolserverPath
-
- if {![string length [checkRunning ToolServer MPSX toolserverPath]]} return
-
- insertText "Welcome to Alpha's MPW shell (using ToolServer via AppleEvents)."
- bind '\r' tclCarriageReturn "MPW"
- carriageReturn
- mpwPrompt
- }
- proc mpwPrompt {} {
- insertText "«mpw» "
- }
-
- proc setMPWMode {} {
- changeMode "MPW"
- }
-
- # tclCarriageReturn
-
-
-
- #=============================================================================
- # Shell Aliases
- #=============================================================================
-
-
- proc l {args} {
- eval [concat "ls -CF" $args]}
-
- proc ll {args} {
- eval [concat "ls -l" $args]}
-
-
- proc wc {args} {
- set totChars 0
- set totLines 0
- set totWords 0
- set args [glob -nocomplain $args]
- foreach file $args {
- set id [open $file]
- set chars [string length [set text [read $id]]]
- set lines [llength [split $text "\n"]]
- set words [llength [split $text]]
- insertText [format "\r%8d%8d%8d $file" $lines $words $chars]
- set totChars [expr $totChars+$chars]
- set totWords [expr $totWords+$words]
- set totLines [expr $totLines+$lines]
- close $id
- }
- if {[llength $args] > 1} {
- insertText [format "\r%8d%8d%8d total" $totLines $totWords $totChars]
- }
- }
-
- ###########################################################################
- # better-cp-mv.tcl -- modification of your routines, by Mark Nagata
- # for Alpha 5.72, 1/04/94
- ###########################################################################
- proc cp args {
- if {[set len [llength $args]] < 2} {
- error "usage: cp <file1> <file2>\r cp <file1> .... <dir>"
- }
- set len [expr $len-1]
- set dir [lindex $args $len]
- if {![regexp {:} $dir] && $dir != ""} {
- set dir ":$dir"
- }
- if {[regexp {:$} $dir]} {
- set dir [string trimright $dir {:}]
- }
- set args [lreplace $args $len $len]
- set files {}
- foreach arg $args {
- append files " " [glob $arg]
- }
- set report ""
- if {[llength $files] == 1} {
- set f [lindex $files 0]
- if {[file exists $dir]} {
- set targ $dir:[file tail $f]
- append report $f\ ->\ $targ \r
- copyFile $f $targ
- } else {
- append report $f\ ->\ $dir \r
- copyFile $f $dir
- }
- } else {
- foreach f $files {
- message [file tail $f]
- set targ $dir:[file tail $f]
- append report $f\ ->\ $targ \r
- if {[catch {copyFile $f $targ} that]} {
- alertnote "Error copying '$f' -> '$targ': $that"
- }
- }
- }
- echo [string trimright $report]
- }
-
- proc mv args {
- if {[set len [llength $args]] < 2} {
- error "usage: mv <file1> <file2>\r mv <file1> .... <dir>"
- }
- set len [expr $len-1]
- if {![regexp {.*[^:]} [lindex $args $len] dir]} {
- set dir [string range [lindex $args $len] 1 end]
- }
- if {![regexp {:} $dir] && $dir != ""} {
- set dir [concat :$dir]}
- set args [lreplace $args $len $len]
- set files {}
- foreach arg $args {
- append files " " [glob $arg]
- }
- set report ""
- if {[llength $files] == 1} {
- set f [lindex $files 0]
- if {[file exists $dir]} {
- set targ $dir:[file tail $f]
- append report $f\ >->\ $targ \r
- moveFile $f $targ
- } else {
- append report $f\ >->\ $dir \r
- moveFile $f $dir
- }
- } else {
- foreach f $files {
- message [file tail $f]
- set targ $dir:[file tail $f]
- append report $f\ >->\ $targ \r
- if {[catch {moveFile $f $targ} that]} {
- alertnote "Error moving '$f' -> '$targ': $that"
- }
- }
- }
- echo [string trimright $report]
- }
-
-
- proc rm args {
- set files {}
- foreach arg $args {
- append files " " [glob $arg]
- }
- foreach f $files {
- message [file tail $f]
- removeFile $f
- }
- }
-
-
-
-
- #================================================================================
-
-
- proc tclFileCompletion {} {
- set silly "*"
- set pos [getPos]
- set res [search -f 0 -i 0 -m 0 -r 1 -n {["\{ \t\r]} [expr $pos - 1]]
- if {[string length $res]} {
- set from [lindex $res 1]
- if {$from < $pos} {
- set pd [pwd]
- set text [getText $from $pos]
- if {[string index $text 0] == ":"} {
- set pd [string trimright $pd ":"]
- }
- if {[catch {glob $pd$text$silly} globbed]} {
- set globbed [glob $text$silly]
- set pd ""
- }
- if {[llength $globbed] == 1} {
- set len [string length $pd$text]
- insertText [string range [lindex $globbed 0] $len end]
- } elseif {[llength $globbed] != 0} {
- set globbed [lsort $globbed]
- set one [lindex $globbed 0]
- set two [lindex $globbed end]
-
- set len [string length $pd$text]
- set one [string range $one $len end]
- set two [string range $two $len end]
-
- set elen [string length $one]
- if {[string length $two] < $elen} {
- set elen [string length $two]
- }
- set len 0
- set str ""
- while {($len < $elen) && ([string match $str[string index $one $len]$silly $two])} {
- append str [string index $one $len]
- incr len
- }
-
- if {!$len} {
- set elen [string length $pd]
- foreach g $globbed {
- lappend short [string range $g $elen end]
- }
- set blah [getText [lineStart [getPos]] [getPos]]
- insertText "\r" $short "\r" $blah
- } else {
- insertText $str
- }
- }
- }
- }
- }
-
-
-
- #================================================================================
- # To prevent ambiguity, 'from' is assumed to be a complete pathname, ending
- # in a directory name. If it doesn't end w/ a colon, one is added. 'to' is
- # assumed to be the parent directory of the top directory we are creating.
- #================================================================================
- proc cpdir {from to} {
- set cwd [pwd]
- if {[string match ":*" $from] || [string match ":*" $to] ||
- ![file exists $from] || ![file exists $to]} {
- error "'cpdir' args must be complete pathnames of existing folders."
- }
- if {![string match "*:" $from]} {append from ":"}
- if {![string match "*:" $to]} {append to ":"}
-
- if {![file isdir $from] || ![file isdir $to]} {
- exit 1
- }
-
- set res [catch {cphier $from $to} val]
- cd $cwd
- if {$res} {error $val}
- }
-
- proc cphier {from to} {
- set savedir [pwd]
- if {[string index $from [expr [string len $from] - 1]] != ":"} {append from ":"}
- set dir [file tail [string trimright $from ":"]]
- cd $to
- mkdir "$dir"
- foreach f [glob "$from*"] {
- if {[file isdir $f]} {
- cphier "$f:" "$to$dir:"
- } else {
- cp $f $to$dir:
- }
- }
- cd $savedir
- }
-
-
- if {![string length [info commands oldMkdir]]} {
- rename mkdir oldMkdir
- rename rmdir oldRmdir
- }
-
- proc mkdir {dir} {
- oldMkdir [list $dir]
- }
-
- proc rmdir {dir} {
- oldRmdir [list $dir]
- }
-
- proc shellBol {} {
- set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
- if {[set ind [string first "» " $text]] > 0} {
- goto [expr [lineStart [getPos]] + $ind + 2]
- } else {
- goto [lineStart [getPos]]
- }
- }
- bind 'a' <z> shellBol Csh
-
-
- proc dummyCsh {} {}
-
- #================================================================================
-
- proc shellup {} {
- set pos [expr [lineStart [getPos]] - 1]
- if {[catch {regexp {∞} [getText [lineStart $pos] [nextLineStart $pos]]} res] || !$res} {
- previousLine; return
- }
- select [lineStart $pos] [nextLineStart $pos]
- }
- bind up shellup Csh
-
-
- proc shelldown {} {
- set pos [nextLineStart [getPos]]
- if {[catch {regexp {∞} [getText $pos [nextLineStart $pos]]} res] || !$res} {
- nextLine; return
- }
- select $pos [nextLineStart $pos]
- }
- bind down shelldown Csh
-
-
- #================================================================================
- proc sortdt {dt} {
- scan $dt "%d/%d/%d {%d:%d:%d %1sM}" mon day yea hou min sec z
- if {$z == "P"} {incr hou 12}
- return [format "%02d%02d%02d%02d%02d" $yea $mon $day $hou $min]
- }
-
-
- proc lt args {
- set val "*"
- set sort 1
- scan [lindex [date] 0] "%d/%d/%d" one two three
- set year 19$three
-
- foreach arg $args {
- switch -- $arg {
- "-t" {set sort 0}
- default {set val $arg}
- }
- }
- set mod ""
- foreach f [eval glob $val] {
- if {[catch {getFileInfo $f info}]} {
- if {$sort} {set mod "0000000000 "}
- lappend text [format "%s%s %8d%8d %6s %5s %4s %s %s\n" $mod "D" "0" "0" "" "" "" "DIR " [file tail $f]]
- continue
- }
- if {$sort} {set mod "[sortdt [mtime $info(modified) s]] "}
- set m [mtime $info(modified) a]
- set zer [lindex $m 0]
- set dat [format "%s %2s" [lindex $zer 1] [string trimright [lindex $zer 2] {,}]]
- if {[lindex $zer 3] == $year} {
- if {[scan [lindex $m 1] "%d:%d:%d %s" one two three am] != 4} {
- error "Didn't get four from scan"
- }
- if {[string length $two] == 1} {set two "0$two"}
- set tm [expr {$am == "AM"} ? $one : [expr $one + 12]]:$two
- } else {
- set tm " [lindex $zer 3]"
- }
- lappend text [format "%sF %8d%8d %s %5s %s %s %s\n" $mod $info(datalen) $info(resourcelen) $dat $tm $info(creator) $info(type) [file tail $f]]
- }
- if {$sort} {
- foreach ln [lsort -de $text] {
- append txt [string range $ln 11 end]
- }
- return [string trimright $txt]
- } else {
- return [string trimright [join $text {}]]
- }
- }
-
- #================================================================================
- proc ps {} {
- foreach p [processes] {
- append text [format "%-25s %4s %10d %10d\r" [lindex $p 0] [lindex $p 1] [lindex $p 2] [lindex $p 3]]
- }
- return [string trimright $text]
- }
-
-
- #================================================================================
- # Recursively make creator of all text files 'ALFA'. Optionally takes a starting
- # dir argument, otherwise starts in current directory.
- proc creator {{dir ":"}} {
- if {![catch {glob -t TEXT $dir*} files]} {
- foreach f $files {
- message $f
- setFileInfo $f creator ALFA
- }
- }
-
- if {![catch {glob $dir*} dirs]} {
- foreach d $dirs {
- if {[file isdir $d]} {creator $d:}
- }
- }
- }
-